package com.wzdigit.wms.wms.controller.TransferWareHouse;

import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.framework.model.response.ErrorEntity;
import com.wzdigit.wms.common.utils.ValidatorUtil;
import com.wzdigit.wms.basic.client.Pager;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.client.request.transfer.CreateRemainDocRequest;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.mid.IfShipnoticeHeaderResponse;
import com.wzdigit.wms.basic.client.response.transfer.CalculateRemainResponse;
import com.wzdigit.wms.basic.client.response.transfer.GdnDtlForIfListResponse;
import com.wzdigit.wms.basic.client.response.transfer.GdnDtlRemainPcResponse;
import com.wzdigit.wms.wms.constant.WmsWareConstant;
import com.wzdigit.wms.wms.service.transfer.GdnRemainService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 发货单-- 前端控制器
 * </p>
 *
 * @author
 * @since 2021-01-12
 */
@RestController
@Api(tags = "发货余留单-PC")
@RequestMapping(value = WmsWareConstant.API_VERSION)
public class GdnRemainController {

    public static final String REQUEST_MODEL = "/gdnremain";

    @Autowired
    private GdnRemainService gdnRemainService;

	/**
	 * 根据来源单号获取发货通知单
	 * 
	 * @param noticeNo
	 * @param pager
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 */
	@ApiOperation(value = "根据来源单号获取发货通知单",notes = "根据来源单号获取发货通知单")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/selectIfShipnoticeHeaderByNoticeNo")
    public PagerInfo<List<IfShipnoticeHeaderResponse>> selectIfShipnoticeHeaderByNoticeNo(
    		@ApiParam(value = "来源发货单单号模糊查询", required = false) 
    		@RequestParam(required = false) String filter, Pager<?> pager,
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return gdnRemainService.selectIfShipnoticeHeaderByNoticeNo(filter, pager);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
	
	/**
	 * 根据来源单号获取发货单明细
	 * 
	 * @param noticeNo
	 * @param pager
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 */
	@ApiOperation(value = "根据来源单号获取发货单明细",notes = "根据来源单号获取发货单明细")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/selectGdnDtlForIfListByNoticeNo")
    public PagerInfo<List<GdnDtlForIfListResponse>> selectGdnDtlForIfListByNoticeNo(
    		@ApiParam(value = "来源发货单单号", required = true) 
    		@RequestParam(required = true) String noticeNo, Pager<?> pager,
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return gdnRemainService.selectGdnDtlForIfListByNoticeNo(noticeNo, pager);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
	
	/**
	 * 根据来源单号/发货单明细获取余留信息
	 * 
	 * @param gdnDtlId
	 * @param noticeNo
	 * @param pager
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 */
	@ApiOperation(value = "根据来源单号/发货单明细获取余留信息",notes = "根据来源单号/发货单明细获取余留信息")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/selectGdnDtlRemainListByGdnDtlIdOrSourceNo")
    public PagerInfo<List<GdnDtlRemainPcResponse>> selectGdnDtlRemainListByGdnDtlIdOrSourceNo(
    		@ApiParam(value = "发货单明细ID", required = false) 
    		@RequestParam(required = false) Integer gdnDtlId, 
    		@ApiParam(value = "来源发货单单号", required = false) 
    		@RequestParam(required = false) String noticeNo, Pager<?> pager,
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return gdnRemainService.selectGdnDtlRemainListByGdnDtlIdOrSourceNo(gdnDtlId, noticeNo, pager);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
	
	/**
	 * 打印余留单execl
	 * @param noticeNo
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 * @throws Exception 
	 */
	@ApiOperation(value = "打印余留单execl",notes = "打印余留单execl")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/loadingRemainPrint")
    public void loadingRemainPrint(
    		@ApiParam(value = "来源发货单单号", required = true) 
    		@RequestParam(required = true) String noticeNo, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) throws Exception {
        try {
        	gdnRemainService.loadingRemainPrint(noticeNo, servletRequest, servletResponse);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }



	/**
	 * 打印余留单execl
	 * @param noticeNo
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(value = "打印余留单execl",notes = "打印余留单execl")
	@ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
	@GetMapping(value = REQUEST_MODEL + "/loadingRemainPrintNew")
	public Result loadingRemainPrintNew(
			@ApiParam(value = "来源发货单单号", required = true)
			@RequestParam(required = true) String noticeNo,
			HttpServletRequest servletRequest,
			HttpServletResponse servletResponse) throws Exception {
		try {
			return  gdnRemainService.loadingRemainPrintNew(noticeNo, servletRequest, servletResponse);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
	/**
	 * 计算余留单
	 * 
	 * @param noticeNo
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 */
	@ApiOperation(value = "计算余留单",notes = "计算余留单")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/calculateRemainDoc")
    public CalculateRemainResponse calculateRemainDoc(
    		@ApiParam(value = "来源发货单单号", required = true) 
    		@RequestParam(required = true) String noticeNo,
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return gdnRemainService.calculateRemainDoc(noticeNo);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
	
	/**
	 * 创建余留单
	 * 
	 * @param dto
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 * @throws Exception 
	 */
	@ApiOperation(value = "创建余留单",notes = "创建余留单")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PostMapping(value = REQUEST_MODEL + "/createRemainDoc")
    public Result<?> createRemainDoc(
    		@RequestBody @Valid CreateRemainDocRequest dto,
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) throws Exception {
        try {
        	ValidatorUtil.validator(bindingResult);
            return gdnRemainService.createRemainDoc(dto, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
}
